package yang.yu.sunny.infra.elasticsearch;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by yyang on 16/4/11.
 */
public class EsClientFactory {


    private String host;

    private int port;

    private String clusterName;

    public EsClientFactory(String host, int port, String clusterName) {
        this.host = host;
        this.port = port;
        this.clusterName = clusterName;
    }

    public Client create() throws UnknownHostException {
        Settings settings = Settings.builder().put("cluster.name", clusterName).build();
        return TransportClient.builder().settings(settings).build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
    }
}
